import 'dart:io';

import 'package:animal/socket/socket_client.dart';
import 'package:animal/ui/start_page.dart';
import 'package:animal/utils/DBUtil.dart';
import 'package:animal/utils/localizations.dart';
import 'package:animal/utils/sputils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() async {
  /// 注意：需要添加下面的一行，才可以使用 异步方法
  WidgetsFlutterBinding.ensureInitialized();
  /// 初始化 Hive
  await DBUtil.install();
  await DBUtil.getInstance();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key) {
    if (Platform.isAndroid) {
      // 以下两行设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
    _init();
  }

  _init() async {
    WidgetsFlutterBinding.ensureInitialized();
    //强制只能竖屏
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  }

  @override
  Widget build(BuildContext context) {
    //初始化 sp
    return AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child: GetMaterialApp(
            title: '火讯',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
                brightness: Brightness.light,
                // fontFamily: "pinfang", // 统一指定应用的字体。
                backgroundColor: Colors.white),
            localizationsDelegates: const [
              //防止长按输入框异常
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              ChineseCupertinoLocalizations.delegate,
            ],
            navigatorObservers: [FlutterSmartDialog.observer],
            supportedLocales: const [
              Locale('zh', 'CN'),
              Locale('en', 'US'),
            ],
            home: const StartPage(),
            builder: (BuildContext? context, Widget? child) {
              FlutterSmartDialog.init();
              _isFirst();
              return MediaQuery(
                //设置文字大小不随系统设置改变
                data: MediaQuery.of(context!).copyWith(textScaleFactor: 0.9),
                child: FlutterSmartDialog(
                    child: GestureDetector(
                        onTap: () {
                          FocusScopeNode currentFocus = FocusScope.of(context);
                          if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                            FocusManager.instance.primaryFocus!.unfocus();
                          }
                        },
                        child: child)),
              );
            }));
  }

  Future<bool?> _isFirst() async {
    await SpUtils().init();
    // return SpUtils().getBool("xieyiApp");
  }
}
